{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原创教程，版权所有。\n",
    "\n",
    "同济子豪兄B站视频专栏：https://space.bilibili.com/1900783\n",
    "\n",
    "玩转UCI心脏病二分类数据集，课件、代码、答疑互动：https://t.zsxq.com/Z7yNZBu\n",
    "\n",
    "子豪兄Python交流QQ群：1077638784\n",
    "\n",
    "子豪兄Kaggle数据科学竞赛交流：481041896\n",
    "\n",
    "微信公众号：人工智能小技巧\n",
    "\n",
    "2020-05-15\n",
    "\n",
    "\n",
    "# 本节概述\n",
    "\n",
    "在uci心脏病数据集上训练随机森林分类模型，在测试集上预测，获得预测结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 构建数据集、训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "# 忽略烦人的红色提示\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "# 忽略烦人的红色提示\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,\n",
       "                       criterion='gini', max_depth=5, max_features='auto',\n",
       "                       max_leaf_nodes=None, max_samples=None,\n",
       "                       min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "                       min_samples_leaf=1, min_samples_split=2,\n",
       "                       min_weight_fraction_leaf=0.0, n_estimators=100,\n",
       "                       n_jobs=None, oob_score=False, random_state=None,\n",
       "                       verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入数据集，划分特征和标签\n",
    "df = pd.read_csv('process_heart.csv')\n",
    "X = df.drop('target',axis=1)\n",
    "y = df['target']\n",
    "\n",
    "# 划分训练集和测试集\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)\n",
    "\n",
    "# 构建随机森林模型\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "model = RandomForestClassifier(max_depth=5, n_estimators=100)\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模型对测试集的预测结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred = model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1,\n",
       "       1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1,\n",
       "       1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred_proba = model.predict_proba(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.83861097, 0.16138903],\n",
       "       [0.54713679, 0.45286321],\n",
       "       [0.66133418, 0.33866582],\n",
       "       [0.22926737, 0.77073263],\n",
       "       [0.74596448, 0.25403552],\n",
       "       [0.26129189, 0.73870811],\n",
       "       [0.45415395, 0.54584605],\n",
       "       [0.35311903, 0.64688097],\n",
       "       [0.12166795, 0.87833205],\n",
       "       [0.86187684, 0.13812316],\n",
       "       [0.04629925, 0.95370075],\n",
       "       [0.82320769, 0.17679231],\n",
       "       [0.41250658, 0.58749342],\n",
       "       [0.15519402, 0.84480598],\n",
       "       [0.80649309, 0.19350691],\n",
       "       [0.16713374, 0.83286626],\n",
       "       [0.902641  , 0.097359  ],\n",
       "       [0.97946855, 0.02053145],\n",
       "       [0.30531956, 0.69468044],\n",
       "       [0.70431739, 0.29568261],\n",
       "       [0.9030481 , 0.0969519 ],\n",
       "       [0.20662684, 0.79337316],\n",
       "       [0.28792199, 0.71207801],\n",
       "       [0.13663512, 0.86336488],\n",
       "       [0.61241705, 0.38758295],\n",
       "       [0.7611255 , 0.2388745 ],\n",
       "       [0.98212677, 0.01787323],\n",
       "       [0.90027653, 0.09972347],\n",
       "       [0.13222523, 0.86777477],\n",
       "       [0.97780203, 0.02219797],\n",
       "       [0.13333374, 0.86666626],\n",
       "       [0.73018862, 0.26981138],\n",
       "       [0.91532899, 0.08467101],\n",
       "       [0.73879415, 0.26120585],\n",
       "       [0.99571397, 0.00428603],\n",
       "       [0.84459156, 0.15540844],\n",
       "       [0.3082504 , 0.6917496 ],\n",
       "       [0.43571749, 0.56428251],\n",
       "       [0.10363846, 0.89636154],\n",
       "       [0.78516179, 0.21483821],\n",
       "       [0.92163046, 0.07836954],\n",
       "       [0.75243602, 0.24756398],\n",
       "       [0.16431584, 0.83568416],\n",
       "       [0.38771429, 0.61228571],\n",
       "       [0.08188002, 0.91811998],\n",
       "       [0.47585048, 0.52414952],\n",
       "       [0.46085926, 0.53914074],\n",
       "       [0.15563937, 0.84436063],\n",
       "       [0.23564848, 0.76435152],\n",
       "       [0.60575072, 0.39424928],\n",
       "       [0.38876528, 0.61123472],\n",
       "       [0.20501934, 0.79498066],\n",
       "       [0.94279473, 0.05720527],\n",
       "       [0.83491732, 0.16508268],\n",
       "       [0.05527031, 0.94472969],\n",
       "       [0.24033775, 0.75966225],\n",
       "       [0.17894678, 0.82105322],\n",
       "       [0.25791298, 0.74208702],\n",
       "       [0.97540935, 0.02459065],\n",
       "       [0.95634865, 0.04365135],\n",
       "       [0.28273216, 0.71726784]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred_proba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.16138903, 0.45286321, 0.33866582, 0.77073263, 0.25403552,\n",
       "       0.73870811, 0.54584605, 0.64688097, 0.87833205, 0.13812316,\n",
       "       0.95370075, 0.17679231, 0.58749342, 0.84480598, 0.19350691,\n",
       "       0.83286626, 0.097359  , 0.02053145, 0.69468044, 0.29568261,\n",
       "       0.0969519 , 0.79337316, 0.71207801, 0.86336488, 0.38758295,\n",
       "       0.2388745 , 0.01787323, 0.09972347, 0.86777477, 0.02219797,\n",
       "       0.86666626, 0.26981138, 0.08467101, 0.26120585, 0.00428603,\n",
       "       0.15540844, 0.6917496 , 0.56428251, 0.89636154, 0.21483821,\n",
       "       0.07836954, 0.24756398, 0.83568416, 0.61228571, 0.91811998,\n",
       "       0.52414952, 0.53914074, 0.84436063, 0.76435152, 0.39424928,\n",
       "       0.61123472, 0.79498066, 0.05720527, 0.16508268, 0.94472969,\n",
       "       0.75966225, 0.82105322, 0.74208702, 0.02459065, 0.04365135,\n",
       "       0.71726784])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred_proba[:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True, False,  True,  True,  True,  True,\n",
       "       False,  True, False,  True,  True, False,  True, False, False,\n",
       "        True, False, False,  True,  True,  True, False, False, False,\n",
       "       False,  True, False,  True, False, False, False, False, False,\n",
       "        True,  True,  True, False, False, False,  True,  True,  True,\n",
       "        True,  True,  True,  True, False,  True,  True, False, False,\n",
       "        True,  True,  True,  True, False, False,  True])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred_proba[:,1] > 0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1,\n",
       "       1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1,\n",
       "       1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
